from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES
import hashlib
import base64
import requests
import json
speakers=	{'刚哥': 65350, '葛二爷': 67230, '小东': 68140, 'Carlos': 20000041,
    '小肥': 68090, 'Sosuke': 20000025, '小峰': 60030, '希涵': 130004,
    '飞飞': 65310, '韦香主': 62070, '瑶瑶': 65360, '晓倩': 10014, 
    'Sidsel': 20000007, 'Jessie': 20000001, '辉叔': 65250, '七哥': 130007,
    'Shirley': 20000029, '李锐': 64470, '台湾玉儿': 68120, '天明': 20079, 
    '小娟': 60026, '百合仙子': 62060, 'Arif': 20000003, '小晨': 200504, 
    '万叔': 65450, '飞碟哥': 20061, '宣哥': 20067, 'Lindsay': 20066, 
    '七哥': 20053, '宁宁': 70002, '小钟': 130001, '萌萌': 60161, '张大大': 64450,
    'Orsi': 20000019, '坤叔': 64010, '小师': 62140, 'Jana': 20000006, 'Liu': 20000005, 
    '小芳': 62020, '子晴': 130003, '一菲': 20073, 'Alicia': 20000040, 
    'Ino': 20000016, '寇乃馨': 64180, '宝哥': 65230, '小玲-测试-默认': 300001, 
    '湖北小王': 20075, '': 0, 'Bento': 20000034, '小俊': 65070, 
    '小露': 130006, 'Olev': 20000009, '小峰': 90030, '小王': 68070, 'Catherine': 69020,
    'Ajda': 20000045, '小爱F11': 62080, '千雪': 20068, '天哥': 20078, 
    'Mathangi': 20000038, '小燕': 60020, 'Wang': 20000004, 'Mila': 20000043, 
    '小璇': 60025, '水哥': 20071, '小师': 20055, '小晚': 20069, 
    'Lando': 20000023, '萌小新': 60170, '刚哥': 20052, '嘉嘉': 65180, 
    '湖南小强': 68010, '晓锐F84': 62120, '孟春': 62040, '何炅': 64410, '小洋': 65010, 
    '小包': 68110, 'Maksim': 20000036, '小桃丸': 60120, '皓宇': 130002, 
    '小钟': 130011, '颖儿': 67100, 'Inna': 20000035, '超哥': 20051, '小强': 10017,
    'Bora': 20000027, 'Sakura': 20000024, '玲姐姐': 60140, '小蓉': 10015, '小乔': 65520, 
    '小彬': 62030, 'Maria': 20000022, 'Hugo': 20000013, '合肥小肥': 20076, 'Annie': 20000030, 
    '豆豆': 130015, '河南小坤': 68030, '小雅': 20062, '小燕': 90020, 
    '讯飞有声小佩': 62240, 'Michale': 20000002, 'Denny': 20000026, 'Dida': 20000021, 'Shivani': 20000017, 
    '小果': 130017, '小晨': 130010, '小宇': 15675, '老马': 60150, 
    'Steve': 69030, '内蒙小包': 20077, '小媛': 60100, 'Trương': 20000047, 
    '一峰': 60190, '一峰': 20072, '楠楠': 60130, '会员权益': 20080, 
    '小露-标准-可爱': 130009, '程程': 65080, '小娟-品质-温情': 130014, 
    'Indah': 20000020, '东北晓倩': 68040, '山东小东': 20074, 
    'Paul': 20000044, '小钟': 70001, '小露': 20070, 'John': 69010, 
    '小鹏': 20082, '晓琳': 68020, '四川小蓉': 68060, 'Marcin': 20000032, 
    '小果': 20065, '一菲': 130012, 'Mr.奥': 69055, 'Jatin': 20000018, 
    'Hyeon': 20000028, '天明': 130005, 'Julia': 20000008, 'Izabel': 20000033, 
    '粤语小月': 60027, 'xx': 20064, '小南': 65340, 'Zara': 20000042, 
    '管哥': 20081, '小娟-品质-甜美': 130013, '小华': 62010, 'Aida': 20000046, 
    '楠楠': 30007, '天明-品质-悲伤': 65272, 'Katrina': 20000010, '张绍刚': 64430, 
    '管哥': 130016, 'Anne': 20000014, '广东小梅': 10003, 
    '阳哥': 65270, 'Emma': 20000012, '小光': 65110, '千雪': 130008, 
    '小薛': 65320, 'Leea': 20000011, '小茹': 60180, 'Suzie': 20000039, 
    '小坤': 10016, 'Natali': 20000031, '彬哥': 65090, '陕西小莹': 68080, '小英': 65040, 
    'Yakov': 20000037, '小魏': 62050, '小晨': 20054, 'Bach': 20000015, '天明-品质-生气': 65271
      }
key='G%.g7"Y&Nf^40Ee<'
aes=AES.new(key.encode('utf-8'),AES.MODE_ECB)
def make(word):
    make_url='http://www.peiyinge.com/web-server/1.0/works_synth_sign'
    md5=hashlib.md5(word.encode("utf-8")).hexdigest()
    work_json='''{"channel":"40000001","synth_text_hash_code":"'''+md5+'''","sessid":"","os":"web"}'''
    work_cipher_text=aes.encrypt(pad(work_json.encode("utf-8"),16))
    work_cipher_text=base64.b64encode(work_cipher_text).decode("utf-8")
    data={'req':work_cipher_text}
    response_data=requests.post(make_url,data=data).json()
    print(response_data)
    return response_data
def parseInfo(data):
    cyphertext=data['body']
    cyphertext=base64.decodebytes(cyphertext.encode('utf-8'))
    plain_text = aes.decrypt(cyphertext).decode('utf-8')
    padding_len = ord(plain_text[len(plain_text) - 1])
    plain_text = plain_text[0:-padding_len]
    data_json=json.loads(plain_text)
    print(data_json)
    return data_json
def get_audio(word,speaker="小桃丸"):
    url='http://proxyweb.peiyinge.com/synth'
    b64_data=make(word)
    sign_data=parseInfo(b64_data)
    params={
        "uid": "",
        "ts": sign_data['time_stamp'],
        "sign": sign_data['sign_text'],
        "vid": speakers[speaker],
        "f": "v2",
        "cc": "0000",
        "sid": "30D9545DB9F234D3800FE6191FCBD00F",
        "volume": "0",
        "speed": "0",
        "content": word,
        "listen": "0"
    }
    res=requests.get(url,params=params)
    print(res.url)
get_audio("杀菌和干煸豆角哈哈不能叠加","韦香主")